package leetcode.Y2024M6.study;

import leetcode.Y2024M6.TreeNode;
import org.junit.Test;

import java.util.*;

public class S129 {
    Deque<TreeNode> stack = new LinkedList<>();
    List<Integer> list = new ArrayList<>();

    public int sumNumbers(TreeNode root) {
        return helper(root,0);

        // 左右节点为空才是叶子节点

    }

    public int helper(TreeNode root, int i) {
        if (root == null) return 0;
        int temp = i * 10 + root.val;
        if (root.left == null && root.right == null) {
            return temp;
        }
        return helper(root.left, temp) + helper(root.right, temp);
    }

    @Test
    public void test() {
        int[] root = {4, 9, 0, 5, 1};
        TreeNode tree = TreeNode.createTree(root);
        int i = sumNumbers(tree);
        System.out.println(i);
    }
}
